package cn.iocoder.yudao.module.jky.service.goodsaudit;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.jky.controller.admin.goodsattribute.vo.GoodsAttributeCreateReqVO;
import cn.iocoder.yudao.module.jky.controller.admin.goodsattribute.vo.GoodsAttributeExportReqVO;
import cn.iocoder.yudao.module.jky.controller.admin.goodsattribute.vo.GoodsAttributePageReqVO;
import cn.iocoder.yudao.module.jky.controller.admin.goodsattribute.vo.GoodsAttributeUpdateReqVO;
import cn.iocoder.yudao.module.jky.convert.goodsattribute.GoodsAttributeConvert;
import cn.iocoder.yudao.module.jky.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsattribute.GoodsAttributeDO;
import cn.iocoder.yudao.module.jky.dal.dataobject.goodsaudit.GoodsAuditDO;
import cn.iocoder.yudao.module.jky.dal.mysql.goodsattribute.GoodsAttributeMapper;
import cn.iocoder.yudao.module.jky.dal.mysql.goodsaudit.GoodsAuditMapper;
import cn.iocoder.yudao.module.jky.service.goodsattribute.GoodsAttributeService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.jky.enums.ErrorCodeConstants.GOODS_ATTRIBUTE_NOT_EXISTS;

/**
 * 商品属性 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
@DS("master")
public class GoodsAuditServiceImpl implements GoodsAuditService {
    @Resource
    private GoodsAuditMapper goodsAuditMapper;

    @Override
    public Long createGoodsAudit(GoodsAuditDO goodsAuditDO) {
        GoodsAuditDO goodsAuditDO1 = goodsAuditMapper.selectOne(new LambdaQueryWrapperX<GoodsAuditDO>().eq(GoodsAuditDO::getGoodsId, goodsAuditDO.getGoodsId()));
        if (Objects.nonNull(goodsAuditDO1)) {
            goodsAuditDO.setId(goodsAuditDO1.getId());
            goodsAuditMapper.updateById(goodsAuditDO);
        } else {
            goodsAuditMapper.insert(goodsAuditDO);
        }
        return goodsAuditDO.getId();
    }

    @Override
    public PageResult<GoodsDO> goodsAudit(PageResult<GoodsDO> goodsDOList) {
        goodsDOList.getList().forEach(item -> {
            item.setPassedOrNot(Boolean.FALSE);
            GoodsAuditDO goodsAuditDO = goodsAuditMapper.selectOne(new LambdaQueryWrapperX<GoodsAuditDO>().eq(GoodsAuditDO::getGoodsId, item.getGoodsId()));
            if (Objects.nonNull(goodsAuditDO)) {
                item.setPassedOrNot(goodsAuditDO.getPassedOrNot());
            }
        });
        return goodsDOList;
    }

    @Override
    public GoodsAuditDO getBean(Integer goodsId) {
        return goodsAuditMapper.selectOne(new LambdaQueryWrapperX<GoodsAuditDO>().eq(GoodsAuditDO::getGoodsId, goodsId));
    }
}
